SelectViewHelper
Eigener ViewHelper für Selects
Der SelectViewHelper rendert ein <select><option=...> Element. Allerdings kann er nicht auf die Werte aus der TCA zugreifen. Das habe ich hinzugefügt - man kann meinem ViewHelper im Parameter options die tca mitgeben und der Viewhelper lädt daraus die passenden Werte aus der tca. :
<rsys:form.select name="motivation" property="motivation" options="{tca:'fe_users'}" />
Das geht jedoch nur für in der TCA definierte Werte - also nicht mit foreign_table Einträgen in der tca. Dies ist analog zum SelectRadioViewHelper
Hier das Listing:
<?php
/* *
* This script is backported from the FLOW3 package "TYPO3.Fluid". *
* *
* It is free software; you can redistribute it and/or modify it under *
* the terms of the GNU Lesser General Public License, either version 3 *
* of the License, or (at your option) any later version. *
* *
* The TYPO3 project - inspiring people to share! *
* */
/**
* This view helper generates a <select> dropdown list for the use with a form.
*
* = Basic usage =
*
* The most straightforward way is to supply an associative array as the "options" parameter.
* The array key is used as option key, and the value is used as human-readable name.
*
* <code title="Basic usage">
* <f:form.select name="paymentOptions" options="{payPal: 'PayPal International Services', visa: 'VISA Card'}" />
* </code>
*
* = Pre-select a value =
*
* To pre-select a value, set "value" to the option key which should be selected.
* <code title="Default value">
* <f:form.select name="paymentOptions" options="{payPal: 'PayPal International Services', visa: 'VISA Card'}" value="visa" />
* </code>
* Generates a dropdown box like above, except that "VISA Card" is selected.
*
* If the select box is a multi-select box (multiple="true"), then "value" can be an array as well.
*
* = Usage on domain objects =
*
* If you want to output domain objects, you can just pass them as array into the "options" parameter.
* To define what domain object value should be used as option key, use the "optionValueField" variable. Same goes for optionLabelField.
* If neither is given, the Identifier (UID/uid) and the __toString() method are tried as fallbacks.
*
* If the optionValueField variable is set, the getter named after that value is used to retrieve the option key.
* If the optionLabelField variable is set, the getter named after that value is used to retrieve the option value.
*
* <code title="Domain objects">
* <f:form.select name="users" options="{userArray}" optionValueField="id" optionLabelField="firstName" />
* </code>
* In the above example, the userArray is an array of "User" domain objects, with no array key specified.
*
* So, in the above example, the method $user->getId() is called to retrieve the key, and $user->getFirstName() to retrieve the displayed value of each entry.
*
* The "value" property now expects a domain object, and tests for object equivalence.
*
* @api
*/
class Tx_Rsysworkbook_ViewHelpers_Form_SelectViewHelper extends Tx_Fluid_ViewHelpers_Form_SelectViewHelper {
/**
* Render the tag.
*
* @return string rendered tag.
* @api
*/
public function render() {
$name = $this->getName();
if ($this->hasArgument('multiple')) {
$name .= '[]';
}
$this->tag->addAttribute('name', $name);
$options = $this->getOptions();
if (empty($options)) {
$options = array('' => '');
}
// rsys tca
if(sizeof($options) == 1) {
// loading tca with options like this: options="{tca:'fe_users'}
if(array_key_exists('tca', $options)) {
// get table name
$tableName = $options['tca'];
// load tca
$GLOBALS['TSFE']->includeTCA();
$tca = t3lib_div::loadTCA($tableName);
$items = $GLOBALS['TCA'][$tableName]['columns'][$this->arguments['property']]['config']['items'];
// clear options array tca:'xyz'
//$options = array('' => '');
$options = array();
// get options
foreach ($items as $value) {
$options[$value[1]]=$value[0];
}
if ($this->arguments['sortByOptionLabel']) {
asort($options);
}
}
}
// rsys: end
$this->tag->setContent($this->renderOptionTags($options));
$this->setErrorClassAttribute();
$content = '';
// register field name for token generation.
// in case it is a multi-select, we need to register the field name
// as often as there are elements in the box
if ($this->hasArgument('multiple') && $this->arguments['multiple'] !== '') {
$content .= $this->renderHiddenFieldForEmptyValue();
for ($i=0; $i<count($options); $i++) {
$this->registerFieldNameForFormTokenGeneration($name);
}
} else {
$this->registerFieldNameForFormTokenGeneration($name);
}
$content .= $this->tag->render();
return $content;
}
}
?>